﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PMS.Logic.BD
{
    public partial class PermissionAjax : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            String methodName = Request["method"];
            Type type = this.GetType();
            MethodInfo method = type.GetMethod(methodName);
            if (method == null) throw new Exception("method is null");
            try
            {

                method.Invoke(this, null);
            }
            catch
            {
                throw;
            }


        }


        public void GetPermissList()
        {

            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }
            Expression<Func<View_BD_Permission, bool>> exp = SearchExpression.FindByGroup<View_BD_Permission>(listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var list = DALSession.Current.View_BD_PermissionDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order);
            count = DALSession.Current.View_BD_PermissionDAL.GetCount(exp);

            Response.Write(JsonHelper.toPageJson(list, count));
        }

        /// <summary>
        /// 修改链接权限
        /// </summary>
        public void EditPermission()
        {
            int Id = Commons.String2Int32(Request.Form["Id"]);
            BD_Permission perNew = DALSession.Current.BD_PermissionDAL.GetListBy(d => d.Id == Id).FirstOrDefault();

            ModelTools.Form2EditModel(Request.Form, perNew);
            perNew.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
            perNew.Clerk = OperateContext.SessBasicInfo.UserInfo.Name;
            DALSession.Current.BD_PermissionDAL.ModifyAllModel(perNew);
            DALSession.Current.BD_PermissionDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }
        /// <summary>
        /// 添加链接权限
        /// </summary>
        public void AddPermission()
        {
            BD_Permission per = new BD_Permission();
            ModelTools.Form2EditModel(Request.Form, per);
            per.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
            per.Clerk = OperateContext.SessBasicInfo.UserInfo.Name;
            DALSession.Current.BD_PermissionDAL.Add(per);
            DALSession.Current.BD_PermissionDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        public void DelPermission()
        {

            //  int Id = Commons.String2Int32(Request.Form["Id"]);
            int Id = Convert.ToInt32(Request.Form["Id"].ToString());
            DALSession.Current.BD_PermissionDAL.DelBy(p => p.Id == Id);
            DALSession.Current.BD_RolePermissionDAL.DelBy(r => r.PId == Id);
            DALSession.Current.BD_UserVipPermissionDAL.DelBy(u => u.vipPermission == Id);
            DALSession.Current.BD_PermissionDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }

        /// <summary>
        /// 获取按钮权限列表
        /// </summary>
        public void GetBtnPermissionList()
        {

            List<SearchCondition> listSearch = new List<SearchCondition>();
            if (Request.Form["searchData"] != null)
            {
                string searchData = Request.Form["searchData"];
                listSearch = ModelTools.GetSearchContions(searchData);
            }

       Expression<Func<View_BD_btnPermission, bool>> exp = SearchExpression.FindByGroup<View_BD_btnPermission>(listSearch);
            string OrderId = OperateContext.Sort == null ? "Id" : OperateContext.Sort;
            int count = 0;
            var btnList = DALSession.Current.View_BD_btnPermissionDAL.GetPagedList(OperateContext.Page, OperateContext.Rows, exp, OrderId, OperateContext.Order).ToList();

            count = DALSession.Current.View_BD_btnPermissionDAL.GetCount(exp);

            Response.Write(JsonHelper.toPageJson(btnList, count));
        }

        /// <summary>
        /// 新增按钮权限
        /// </summary>
        public void AddBtnPermission()
        {

            BD_btnPermission per = new BD_btnPermission();
            ModelTools.Form2EditModel(Request.Form, per);
            per.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
            DALSession.Current.BD_btnPermissionDAL.Add(per);
            DALSession.Current.BD_btnPermissionDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }


        /// <summary>
        /// 修改按钮权限
        /// </summary>
        public void EditBtnPermission()
        {
            int Id = Commons.String2Int32(Request.Form["Id"]);
            BD_btnPermission perNew = DALSession.Current.BD_btnPermissionDAL.GetListBy(d => d.Id == Id).FirstOrDefault();
           
            ModelTools.Form2EditModel(Request.Form, perNew);
            perNew.AddTime = DateTime.Now.ToString("yyyy-MM-dd");
            DALSession.Current.BD_btnPermissionDAL.ModifyAllModel(perNew);
            DALSession.Current.BD_PermissionDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "保存成功"));
        }

        /// <summary>
        /// 删除按钮权限
        /// </summary>
        public void DelBtnPermission()
        {

            //  int Id = Commons.String2Int32(Request.Form["Id"]);
            int Id = Convert.ToInt32(Request.Form["Id"].ToString());
            DALSession.Current.BD_btnPermissionDAL.DelBy(p => p.Id == Id);
            DALSession.Current.BD_RoleBtnPermissionDAL.DelBy(r => r.BId == Id);
            DALSession.Current.BD_UserVipBtnPermissionDAL.DelBy(u => u.vipBtnPermission == Id);
            DALSession.Current.BD_PermissionDAL.SaveChanges();
            Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        }
        ///// <summary>
        ///// 验证
        ///// </summary>
        //public void test()
        //{

        //    //  int Id = Commons.String2Int32(Request.Form["Id"]);
        //    int Id = Convert.ToInt32(Request.Form["Id"].ToString());
        //    DALSession.Current.BD_btnPermissionDAL.DelBy(p => p.Id == Id);
        //    DALSession.Current.BD_RoleBtnPermissionDAL.DelBy(r => r.BId == Id);
        //    DALSession.Current.BD_UserVipBtnPermissionDAL.DelBy(u => u.vipBtnPermission == Id);
        //    DALSession.Current.BD_PermissionDAL.SaveChanges();
        //    Response.Write(JsonHelper.toDefaultMsg(true, "删除成功"));
        //}

    }
}